package com.lzymall.controller.backend;

import com.github.pagehelper.PageInfo;
import com.lzymall.common.Const;
import com.lzymall.common.ResponseCode;
import com.lzymall.common.ServiceResponse;
import com.lzymall.pojo.User;
import com.lzymall.service.IOrderService;
import com.lzymall.service.IUserService;
import com.lzymall.util.CookieUtil;
import com.lzymall.util.JsonUtil;
import com.lzymall.util.RedisShardedPoolUtil;
import com.lzymall.vo.OrderVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author: lizhenyao
 * @Desciption:
 */
@Controller
@RequestMapping("/manage/order")
public class OrderManageController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IOrderService iOrderService;

    @RequestMapping("list.do")
    @ResponseBody
    public ServiceResponse<PageInfo> orderList(HttpServletRequest request, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                               @RequestParam(value = "pageSize", defaultValue = "10")int pageSize){
//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isBlank(loginToken)){
//            return ServiceResponse.createByErrorMessage("用户未登录,无法获取到当前的用户信息");
//        }
//        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonStr, User.class);
//        if (user == null){
//            return  ServiceResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录");
//        }
//        if (iUserService.checkAdminRole(user).isSuccess()){
//            return iOrderService.manageList(pageNum, pageSize);
//        }
//        return ServiceResponse.createByErrorMessage("无权限操作,需要管理员权限");

        //权限判断是否登录或者管理的逻辑交给了拦截器来处理
        return iOrderService.manageList(pageNum, pageSize);
    }

    @RequestMapping("detail.do")
    @ResponseBody
    public ServiceResponse<OrderVo> orderDetail(HttpServletRequest request, Long orderNo){
//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isBlank(loginToken)){
//            return ServiceResponse.createByErrorMessage("用户未登录,无法获取到当前的用户信息");
//        }
//        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonStr, User.class);
//        if (user == null){
//            return  ServiceResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录");
//        }
//        if (iUserService.checkAdminRole(user).isSuccess()){
//            return iOrderService.manageDetail(orderNo);
//        }
//        return ServiceResponse.createByErrorMessage("无权限操作,需要管理员权限");
        //权限判断是否登录或者管理的逻辑交给了拦截器来处理
        return iOrderService.manageDetail(orderNo);

    }

    @RequestMapping("search.do")
    @ResponseBody
    public ServiceResponse<PageInfo> orderSearch(HttpServletRequest request, Long orderNo, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                               @RequestParam(value = "pageSize", defaultValue = "10")int pageSize){
//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isBlank(loginToken)){
//            return ServiceResponse.createByErrorMessage("用户未登录,无法获取到当前的用户信息");
//        }
//        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonStr, User.class);
//        if (user == null){
//            return  ServiceResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录");
//        }
//        if (iUserService.checkAdminRole(user).isSuccess()){
//            return iOrderService.manageSearch(orderNo, pageNum, pageSize);
//        }
//        return ServiceResponse.createByErrorMessage("无权限操作,需要管理员权限");

        //权限判断是否登录或者管理的逻辑交给了拦截器来处理
        return iOrderService.manageSearch(orderNo, pageNum, pageSize);
    }

    @RequestMapping("send_goods.do")
    @ResponseBody
    public ServiceResponse<String> orderSendGoods(HttpServletRequest request, Long orderNo){
//        String loginToken = CookieUtil.readLoginToken(request);
//        if (StringUtils.isBlank(loginToken)){
//            return ServiceResponse.createByErrorMessage("用户未登录,无法获取到当前的用户信息");
//        }
//        String userJsonStr = RedisShardedPoolUtil.get(loginToken);
//        User user = JsonUtil.string2Obj(userJsonStr, User.class);
//        if (user == null){
//            return  ServiceResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录");
//        }
//        if (iUserService.checkAdminRole(user).isSuccess()){
//            return iOrderService.manageSendGoods(orderNo);
//        }
//        return ServiceResponse.createByErrorMessage("无权限操作,需要管理员权限");

        //权限判断是否登录或者管理的逻辑交给了拦截器来处理
        return iOrderService.manageSendGoods(orderNo);
    }

}
